package cn.ges.android;


import com.meituan.android.walle.ChannelReader;
import com.meituan.android.walle.ChannelWriter;
import com.meituan.android.walle.SignatureNotFoundException;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 安卓添加包信息工具
 *
 * @author gespent@163.com
 * @date 2019/5/15
 */
@Slf4j
public class AddMsgUtil {


    public static Map<String, String> getMsg(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return new HashMap<>();
        }
        return ChannelReader.getMap(file);
    }

    public static void addMsg(@NonNull String apkPath, @NonNull String outPath, @NonNull String channel, HashMap<String, String> otherMsg) throws IOException {
        // copy原文件
        File file = new File(apkPath);
        File file1 = new File(outPath);
        FileUtils.copyFile(file, file1);
        // 塞入参数到新的文件
        try {
            ChannelWriter.put(file1, channel, otherMsg);
        } catch (SignatureNotFoundException e) {
            log.info("打包时出现错误,信息为[{}]", e.getMessage());
        }
    }

}
